//
//  LXChooseCarViewViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/20.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXChooseCarViewViewController.h"
@interface LXChooseCarViewViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic) LXTopSearchView *searchView;
@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger pageIndex;
@property (nonatomic) NSInteger totalPage;
@property (nonatomic) NSString *searchStr;
@end

@implementation LXChooseCarViewViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self layoutCustomNav:self.view title:@"车辆列表"];
     
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = ColorWithF5F5F5;
    
    
    LXTopSearchView *searchView = [[LXTopSearchView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, kScreenWidth, 70)];
    [self.view addSubview:searchView];
    searchView.textField.placeholder = @"请输入车牌号查询";
    [searchView setClickBtnBlock:^(NSString * _Nonnull text) {
        self.searchStr = text;
        [self.tableView.mj_header beginRefreshing];
    }];
    self.searchView = searchView;
    
    [self.view addSubview:self.tableView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self loadData];
    }];

    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.pageIndex++;
        [self loadData];
    }];
    
    
}
- (void)loadData{

    NSString *url = [NSString stringWithFormat:@"%@",CarList_URL];
    
    NSMutableDictionary *dic = @{
        @"page":@(self.pageIndex),
        @"rows":@(20),
    }.mutableCopy;
        
    if (!StringIsEmpty(self.searchStr)) {
        [dic setValue:self.searchStr forKey:@"carNum"];
    }
    
    if (!StringIsEmpty(self.type)) {
        [dic setValue:self.type forKey:@"type"];
    }

    if (!StringIsEmpty(self.signType)) {
        [dic setValue:self.signType forKey:@"signType"];
    }
    [MSNetworkManager postRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        
        if ([self.tableView.mj_header isRefreshing]||self.pageIndex==1) {
            [self.data removeAllObjects];
            [self.tableView.mj_footer resetNoMoreData];
        }
        self.totalPage = [resonseObj[@"total"] integerValue];
            
        NSArray *list = resonseObj[@"list"];
        
        [self.data addObjectsFromArray:[CheLiangModel  mj_objectArrayWithKeyValuesArray:list]];
        
        
        if (self.data.count>=self.totalPage) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
        else {
            self.tableView.mj_footer.state = MJRefreshStateIdle;
        }
        
            
        [self.tableView reloadData];
            
        [self isHaveEmptyData];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LXChooseCarCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXChooseCarCell class]) forIndexPath:indexPath];
    
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([UITableViewCell class])];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.textLabel.font = [UIFont systemFontOfSize:15];
//        cell.textLabel.textColor = Color333;
//        cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
//        cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#AAAAAA"];
//        [cell setClipsToBounds:YES];
//        cell.detailTextLabel.numberOfLines = 0;
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//
//    }
    
    
    CheLiangModel *model = self.data[indexPath.row];
    
    
  
    
    cell.carNumLab.text = model.carNum;
    cell.carBrandLab.text = [NSString stringWithFormat:@"%@-%@",model.carBrand, model.carMode];
    if (StringIsEmpty(model.signContractId)) {
        cell.tagLab.hidden = YES;
    }
    else {
        cell.tagLab.hidden = NO;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CheLiangModel *model = self.data[indexPath.row];
    
    if (self.comfrom == 1) {
        
        if (self.ToSendValue) {
           
            self.ToSendValue(model);
            [self.navigationController popViewControllerAnimated:YES];
        }

    }
    else {
        if ([model.hireFlag intValue] == 1) {
            showAlert(@"该车辆签约中");
            return;
        }
       
        if (self.ToSendValue) {
           
            self.ToSendValue(model);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
   
}

#pragma mark -- 点击搜索按钮
-(void)clickBtn{

    [self.tableView.mj_header beginRefreshing];
    
}


- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, MaxY(self.searchView)+5, kScreenWidth, kScreenHeight - MaxY(self.searchView)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = kComHeight;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = ColorWithF5F5F5;
        _tableView.rowHeight = 40;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[LXChooseCarCell class] forCellReuseIdentifier:NSStringFromClass([LXChooseCarCell class])];
    }
    return _tableView;
}

- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}


@end

@implementation LXChooseCarCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.carNumLab];
        [self.contentView addSubview:self.carBrandLab];
        [self.contentView addSubview:self.tagLab];
        
    }
    return self;
}
- (UILabel *)carBrandLab{
    if (!_carBrandLab) {
        _carBrandLab = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - 300, 0, 288, 40)];
        _carBrandLab.textAlignment = NSTextAlignmentRight;
        _carBrandLab.textColor = ColorWithAAAAAA;
        _carBrandLab.font = [UIFont systemFontOfSize:14];
    }
    return _carBrandLab;
}
- (UILabel *)carNumLab{
    if (!_carNumLab) {
        _carNumLab = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 85, 40)];
        _carNumLab.textColor = Color333;
        _carNumLab.adjustsFontSizeToFitWidth = YES;
        _carNumLab.font = [UIFont systemFontOfSize:14];
    }
    return _carNumLab;
}
- (UILabel *)tagLab{
    if (!_tagLab) {
        _tagLab = [[UILabel alloc] initWithFrame:CGRectMake(self.carNumLab.X_width, 10, 42, 20)];
        [_tagLab addCorner:2];
        _tagLab.backgroundColor = [mainColor colorWithAlphaComponent:0.1];
        _tagLab.text = @"签约中";
        _tagLab.textAlignment = NSTextAlignmentCenter;
        _tagLab.textColor = mainColor;
        _tagLab.font = [UIFont systemFontOfSize:12];
    }
    return _tagLab;
}

@end
